package com.netty.LongConnect;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author: sun.hongliang
 * @create: 2019/02/22 18:40
 */
public class MessageDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        //标记读的位置
        in.markReaderIndex();
        byte infoType = in.readByte();
        RequestInfo requestInfo = new RequestInfo();
        System.out.println("MessaeDecode Type=" + infoType);
        requestInfo.setType(infoType);
        byte[] info = new byte[in.readableBytes()];
        in.readBytes(info);
        requestInfo.setInfo(new String(info, "utf-8"));
        System.out.println("MessaeDecode info=" + info.length + " : " + requestInfo.getInfo());
        out.add(requestInfo);
    }
}
